package homework2

import scala.collection.mutable.Map
import scala.io.StdIn
import scala.util.Random

object GuessDemo {

  val roleMap = Map(1 -> "刘备", 2 -> "关羽", 3 -> "张飞")
  val gameMap = Map(1 -> "剪刀", 2 -> "石头", 3 -> "布")

  var roleIndex = 0
  var scoreMap = Map("score" -> 0, "win" -> 0, "draw" -> 0, "lose" -> 0)
  var roleScoreMap = Map("score" -> 0, "win" -> 0, "draw" -> 0, "lose" -> 0)

  def main(args: Array[String]): Unit = {

    println("----------------欢迎进入游戏世界-----------------")
    println("***********************************************")
    println("******************猜拳，开始********************")
    println("***********************************************")

    var chooseOk = false
    while (!chooseOk) {
      chooseOk = chooseRole()
    }

    var flagGame = true
    var count = 0
    while (flagGame) {
      flagGame = game()
      count += 1
    }

    val role = roleMap(roleIndex)
    println("退出游戏！")
    println("-----------------------------------------------")
    println(s"$role VS 游客")
    println(s"对战次数${count}次")
    println()
    println()
    println("姓名\t等分\t胜局\t和局\t负局")
    println(s"游客\t${scoreMap("score")}\t\t${scoreMap("win")}\t\t${scoreMap("draw")}\t\t${scoreMap("lose")}")
    println(s"$role\t${roleScoreMap("score")}\t\t${roleScoreMap("win")}\t\t${roleScoreMap("draw")}\t\t${roleScoreMap("lose")}")
    println("-----------------------------------------------")
  }

  /**
   * 选择比赛角色
   *
   * @return
   */
  def chooseRole(): Boolean = {
    println("请选择对战角色:(1.刘备 2.关羽 3.张飞)")
    roleIndex = StdIn.readInt()
    if (!roleMap.contains(roleIndex)) {
      roleIndex = 3
    }
    println(s"你选择了与${roleMap(roleIndex)}对战")
    println("要开始么？ y/n")
    val flag = StdIn.readChar()
    flag match {
      case 'y' => true
      case _ => false
    }
  }

  /**
   * 猜拳
   * @return
   */
  def game(): Boolean = {
    println("请出拳! 1.剪刀 2.石头 3.布")
    var gameIndex = StdIn.readInt()

    gameIndex match {
      case 1 => println(s"你出拳:剪刀")
      case 2 => println(s"你出拳:石头")
      case 3 => println(s"你出拳:布")
      case _ => {
        println(s"输入不符合规范，默认出布！")
        gameIndex = 3
      }
    }
    val roleGameIndex = Random.nextInt(3) + 1
    println(s"${roleMap(roleIndex)}出拳:${gameMap(roleGameIndex)}")
    getScore(gameIndex, roleGameIndex)
    println("是否开始下一轮(y/n)")
    val flag = StdIn.readChar()
    flag match {
      case 'y' => true
      case _ => false
    }
  }

  def getScore(gameIndex: Int, roleGameIndex: Int): Unit = {
    if (gameIndex == roleGameIndex) {
      println("结果：和局!  下次继续努力!")
      scoreMap("score") = scoreMap.getOrElse("score", 0) + 1
      scoreMap("draw") = scoreMap.getOrElse("draw", 0) + 1
      roleScoreMap("score") = roleScoreMap.getOrElse("score", 0) + 1
      roleScoreMap("draw") = roleScoreMap.getOrElse("draw", 0) + 1
    } else if (gameIndex - roleGameIndex == 1 || roleGameIndex - gameIndex == 2) {
      println("恭喜，你赢啦!")
      scoreMap("score") = scoreMap.getOrElse("score", 0) + 2
      scoreMap("win") = scoreMap.getOrElse("win", 0) + 1
      roleScoreMap("lose") = roleScoreMap.getOrElse("lose", 0) + 1
    } else {
      println("你输了!  下次继续努力!")
      roleScoreMap("score") = roleScoreMap.getOrElse("score", 0) + 2
      roleScoreMap("win") = roleScoreMap.getOrElse("win", 0) + 1
      scoreMap("lose") = scoreMap.getOrElse("lose", 0) + 1
    }
  }

}
